package com.grep.base.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.grep.base.db.DBUtils;
import com.grep.extra.factory.utils.FileLoaderUtil;

public class BaseSevlet extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(BaseSevlet.class);    
	private final static String DB_KEY = "dbconfig" ;
	private final static String LOG_KEY = "logconfig" ;
	public void destroy() {
		super.destroy();
		logger.info("[datasource destroy] datasource destroy") ;
		DBUtils.destroy() ;
	}

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		//��ȡ���ò���
		Map<String,String> paramers = FileLoaderUtil.getServerConfigPara(config.getServletContext());
		String dbconfig = paramers.get(DB_KEY) ;
		String logconfig = paramers.get(LOG_KEY) ;
		//��־����
		if(logconfig ==  null ){
			logger.error("[log config] please config log info");
		}else{
			logger.info("[log config] log config file path:{}",logconfig);
			FileLoaderUtil.resetLogProperties(logconfig);
		}
		//��ݿ�����
		if(dbconfig ==  null ){
			logger.error("[db config] please config database info");
		}else{
			logger.info("[db config] datebase config file path:{}",dbconfig);
			DBUtils.setPro(FileLoaderUtil.loadDatabaseProperties(dbconfig)) ;
			DBUtils.init() ;
		}
	}

	protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException {
		super.service(arg0, arg1);
	}
	

}
